<template>
  <view class="ss-flex-col">
    <view class="goods-box" v-for="item in pagination.data" :key="item.id">
      <s-goods-column
        size="sl"
        :data="item"
        :userInfo="state.userInfo"
        @click="handleGoodsClick(item)"
      ></s-goods-column>
    </view>
  </view>
</template>

<script setup>
  import sheep from '@/sheep';
  import { reactive, onMounted } from 'vue';
  
  const state = reactive({
    userInfo: {}
  });
  
  const props = defineProps({
    data: {
      type: Object,
      default: () => ({}),
    },
    activeMenu: [Number, String],
    pagination: Object,
  });

  const emit = defineEmits(['goodsClick', 'showAddCart']);

  // 获取用户信息
  const getUserInfo = async () => {
    try {
      const isLogin = sheep.$store('user').isLogin;
      if (isLogin) {
        const userInfo = await sheep.$store('user').getInfo();
        state.userInfo = userInfo || {};
      } else {
        state.userInfo = {
          level: 0,
          level_text: '普通用户'
        };
      }
    } catch (error) {
      console.error('获取用户信息失败:', error);
      state.userInfo = {
        level: 0,
        level_text: '普通用户'
      };
    }
  };

  const handleGoodsClick = (item) => {
    emit('goodsClick', item);
  }

  // 页面加载时获取用户信息
  onMounted(() => {
    getUserInfo();
  });;
</script>

<style lang="scss" scoped>
  .goods-box {
    width: 100%;
  }
</style>
